<?php
// 定义紫微斗数的主要星曜
$mainStars = [
    "紫微星", "天机星", "太阳星", "武曲星", "天同星",
    "廉贞星", "天府星", "太阴星", "贪狼星", "巨门星",
    "天相星", "天梁星", "七杀星", "破军星"
];

// 定义十二宫位
$palaces = [
    "命宫", "兄弟宫", "夫妻宫", "子女宫",
    "财帛宫", "疾厄宫", "迁移宫", "交友宫",
    "事业宫", "田宅宫", "福德宫", "父母宫"
];

// 随机生成命盘
function generateChart($mainStars, $palaces) {
    $chart = [];
    foreach ($palaces as $palace) {
        $starCount = rand(1, 3);
        $palaceStars = [];
        for ($i = 0; $i < $starCount; $i++) {
            $randomStarIndex = array_rand($mainStars);
            $palaceStars[] = $mainStars[$randomStarIndex];
        }
        $chart[$palace] = $palaceStars;
    }
    return $chart;
}

// 处理生成命盘的请求
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $chart = generateChart($mainStars, $palaces);
}
?>

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>紫微斗数命盘网页游戏</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            text-align: center;
            background-color: #f4f4f4;
        }

        h1 {
            color: #333;
        }

        form {
            margin-top: 20px;
        }

        button {
            padding: 10px 20px;
            font-size: 16px;
            background-color: #007BFF;
            color: white;
            border: none;
            cursor: pointer;
            border-radius: 5px;
        }

        button:hover {
            background-color: #0056b3;
        }

        #chart {
            margin-top: 20px;
            padding: 20px;
            background-color: white;
            border-radius: 5px;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
            display: inline-block;
            text-align: left;
        }

        #chart h2 {
            margin-bottom: 10px;
        }

        #chart ul {
            list-style-type: none;
            padding: 0;
        }

        #chart li {
            margin-bottom: 5px;
        }
    </style>
</head>

<body>
    <h1>紫微斗数命盘网页游戏</h1>
    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
        <button type="submit">生成命盘</button>
    </form>
    <?php
    if (isset($chart)) {
        echo '<div id="chart">';
        foreach ($chart as $palace => $stars) {
            echo '<h2>' . $palace. '</h2>';
            echo '<ul>';
            foreach ($stars as $star) {
                echo '<li>' . $star. '</li>';
            }
            echo '</ul>';
        }
        echo '</div>';
    }
    ?>
</body>

</html>